using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraBars;

namespace NAGFwk.UI
{
    public class BaseModule : BaseControl, ISupportNavigation
    {
        public virtual void Init(BarManager menuManager) { }
        #region ISupportNavigation Members
        public virtual void OnNavigatedFrom(INavigationArgs args) { }
        public virtual void OnNavigatedTo(INavigationArgs args) { }
        #endregion

        protected string partName = string.Empty;

        public BaseModule() { }

        internal FrmMain OwnerForm { get { return FindForm() as FrmMain; } }
        protected RibbonControl MainRibbon { get { return OwnerForm.Ribbon; } }

        public virtual void ShowModule(bool firstShow)
        {
            if (OwnerForm == null)
                return;
            if (AutoMergeRibbon && ChildRibbon != null)
            {
                OwnerForm.Ribbon.MergeRibbon(ChildRibbon);

                RibbonPage page = OwnerForm.Ribbon.Pages.GetPageByName("rpView");
                if (page != null)
                {
                    OwnerForm.Ribbon.MergedPages.Remove(page);
                    OwnerForm.Ribbon.MergedPages.Insert(OwnerForm.Ribbon.MergedPages.Count, page);
                }
                if (ChildRibbonStatusBar != null)
                {
                    OwnerForm.RibbonStatusBar.MergeStatusBar(ChildRibbonStatusBar);
                    OwnerForm.ShowInfo(false);
                }
            }
            OwnerForm.SaveAsMenuItem.Enabled = SaveAsEnable;
            OwnerForm.SaveAttachmentMenuItem.Enabled = SaveAttachmentEnable;
            ShowReminder();
            ShowInfo();
            OwnerForm.ZoomManager.ZoomFactor = (int)(ZoomFactor * 100);
            SetZoomCaption();
            OwnerForm.EnableZoomControl(AllowZoomControl);
            OwnerForm.OnModuleShown(this);
        }
        internal virtual void FocusObject(object obj) { }
        protected virtual void ShowReminder()
        {
            if (OwnerForm != null)
                OwnerForm.ShowReminder(null);
        }
        public void ShowInfo()
        {
            if (OwnerForm == null)
                return;
            if (Grid == null)
            {
                OwnerForm.ShowInfo(null);
                return;
            }
            ICollection list = Grid.DataSource as ICollection;
            if (list == null)
                OwnerForm.ShowInfo(null);
            else
                OwnerForm.ShowInfo(list.Count);
        }
        public virtual void HideModule()
        {
            if (AutoMergeRibbon && OwnerForm != null)
            {
                if (OwnerForm.Ribbon.MergedRibbon == ChildRibbon)
                {
                    RibbonPage page = OwnerForm.Ribbon.MergedPages.GetPageByName("rpView");
                    if (page != null) OwnerForm.Ribbon.Pages.Add(page);
                    OwnerForm.Ribbon.UnMergeRibbon();
                }
                OwnerForm.RibbonStatusBar.UnMergeStatusBar();
                OwnerForm.ShowInfo(true);               
            }
        }
        public virtual void InitModule(IDXMenuManager manager, object data)
        {
            SetMenuManager(Controls, manager);
            if (Grid != null && Grid.MainView is ColumnView)
            {
                ((ColumnView)Grid.MainView).ColumnFilterChanged += BaseModule_ColumnFilterChanged;
            }
            CapitalizeChildRibbonPages();
        }
        void CapitalizeChildRibbonPages()
        {
            if (ChildRibbon == null)
                return;
            foreach (RibbonPage page in ChildRibbon.Pages)
                page.Text = page.Text.ToUpper();
            foreach (RibbonPageCategory category in ChildRibbon.PageCategories)
            {
                foreach (RibbonPage page in category.Pages)
                    page.Text = page.Text.ToUpper();
            }
        }
        public void ShowInfo(ColumnView view)
        {
            if (OwnerForm == null) return;
            ShowReminder();
            OwnerForm.ShowInfo(view.DataRowCount);
        }
        void BaseModule_ColumnFilterChanged(object sender, EventArgs e)
        {
            ShowInfo(sender as ColumnView);
        }
        void SetMenuManager(ControlCollection controlCollection, IDXMenuManager manager)
        {
            foreach (Control ctrl in controlCollection)
            {
                GridControl grid = ctrl as GridControl;
                if (grid != null)
                {
                    grid.MenuManager = manager;
                    break;
                }
                PivotGridControl pivot = ctrl as PivotGridControl;
                if (pivot != null)
                {
                    pivot.MenuManager = manager;
                    break;
                }
                BaseEdit edit = ctrl as BaseEdit;
                if (edit != null)
                {
                    edit.MenuManager = manager;
                    break;
                }
                SetMenuManager(ctrl.Controls, manager);
            }
        }
        RibbonControl FindRibbon(ControlCollection controls)
        {
            RibbonControl res = controls.OfType<Control>().FirstOrDefault(x => x is RibbonControl) as RibbonControl;
            if (res != null)
                return res;
            foreach (Control control in controls)
            {
                if (control.HasChildren)
                {
                    res = FindRibbon(control.Controls);
                    if (res != null)
                        return res;
                }
            }
            return null;
        }

        protected virtual bool AllowZoomControl { get { return false; } }
        protected virtual void SetZoomCaption() { }
        public virtual float ZoomFactor
        {
            get { return 1; }
            set { }
        }
        public virtual IPrintable PrintableComponent { get { return Grid; } }
        public virtual IPrintable ExportComponent { get { return Grid; } }
        public virtual bool AllowRtfTitle { get { return true; } }
        protected virtual GridControl Grid { get { return null; } }
        protected virtual bool SaveAsEnable { get { return false; } }
        protected virtual bool SaveAttachmentEnable { get { return false; } }
        protected virtual bool SaveCalendarVisible { get { return false; } }
        protected internal virtual void ButtonClick(string tag) { }
        protected internal virtual void SendKeyDown(KeyEventArgs e) { }
        protected internal virtual RichEditControl CurrentRichEdit { get { return null; } }
        public virtual string ModuleName { get { return GetType().Name; } }
        public string PartName { get { return partName; } }
        [DefaultValue(false)]
        protected virtual bool AutoMergeRibbon { get; private set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        protected virtual RibbonControl ChildRibbon
        {
            get
            {
                if (!AutoMergeRibbon)
                    return null;
                return FindRibbon(Controls);
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        protected virtual RibbonStatusBar ChildRibbonStatusBar
        {
            get
            {
                if (ChildRibbon != null) return ChildRibbon.StatusBar;
                return null;
            }
        }

        private void InitializeComponent()
        {
            SuspendLayout();
            // 
            // BaseModule
            // 
            Name = "BaseModule";
            ResumeLayout(false);

        }
    }
}
